package mylittleinvestigations;

import mylittleinvestigations.CaseInformation.CaseParseException;
import mylittleinvestigations.CaseInformation.CaseXmlWriter;
import mylittleinvestigations.CaseInformation.Case;
import mylittleinvestigations.CaseInformation.CaseXmlReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class Partner
{
    private class PartnerConversation
    {
        private Condition condition;
        private Conversation conversation;
        
        public PartnerConversation(Condition condition, Conversation conversation)
        {
            this.condition = condition;
            this.conversation = conversation;
        }
        
        public Condition getCondition() { return this.condition; }
        public Conversation getConveration() { return this.conversation; }
    }
    
    private class FieldCursorDefinition
    {
        private final String animationId;
        private final String sfxId;
        
        public FieldCursorDefinition(String animationId, String sfxId)
        {
            this.animationId = animationId;
            this.sfxId = sfxId;
        }
        
        public String getAnimationId() { return this.animationId; }
        
        public void SaveToXml(CaseXmlWriter writer) throws IOException
        {
            writer.StartElement("FieldCursorDefinition");
            
            writer.WriteTextElement("AnimationId", this.animationId);
            
            if (this.sfxId != null)
            {
                writer.WriteTextElement("SfxId", this.sfxId);
            }
            
            writer.EndElement();
        }
    }
    
    private Sprite iconSprite;
    private Sprite profileImageSprite;
    private ArrayList<PartnerConversation> conversationList;
    private HashMap<FieldCursorState, FieldCursorDefinition> fieldCursorDefinitionsByState;

    public Partner(String id, String name)
    {
        this.id = id;
        this.name = name;
        
        this.conversationList = new ArrayList<PartnerConversation>();
        this.fieldCursorDefinitionsByState = new HashMap<FieldCursorState, FieldCursorDefinition>();
        
        this.clickPointOffset = null;
    }
    
    public void AddConversation(Condition condition, Conversation conversation)
    {
        this.conversationList.add(new PartnerConversation(condition, conversation));
    }
    
    public void AddFieldCursorDefinition(FieldCursorState state, String animationId, String sfxId)
    {
        this.fieldCursorDefinitionsByState.put(state, new FieldCursorDefinition(animationId, sfxId));
    }
    
    private String id;
    
    public String getId() { return this.id; }
    public void setId(String id) { this.id = id; }
    
    private String name;
    
    public String getName() { return this.name; }
    public void setName(String name) { this.name = name; }
    
    private String iconSpriteId;
    
    public String getIconSpriteId() { return this.iconSpriteId; }
    public void setIconSpriteId(String iconSpriteId) { this.iconSpriteId = iconSpriteId; }

    public Sprite getIconSprite()
    {
        if (this.iconSprite == null)
        {
            this.iconSprite = Case.getInstance().getSpriteManager().GetSpriteFromId(this.getIconSpriteId());
        }

        return this.iconSprite;
    }
    
    private String profileImageSpriteId;
    
    public String getProfileImageSpriteId() { return this.profileImageSpriteId; }
    public void setProfileImageSpriteId(String profileImageSpriteId) { this.profileImageSpriteId = profileImageSpriteId; }

    public Sprite getProfileImageSprite()
    {
        if (this.profileImageSprite == null)
        {
            this.profileImageSprite = Case.getInstance().getSpriteManager().GetSpriteFromId(this.getProfileImageSpriteId());
        }

        return this.profileImageSprite;
    }
    
    private String passiveAbilityDescription;
    
    public String getPassiveAbilityDescription() { return this.passiveAbilityDescription; }
    public void setPassiveAbilityDescription(String passiveAbilityDescription) { this.passiveAbilityDescription = passiveAbilityDescription; }
    
    private String activeAbilityDescription;
    
    public String getActiveAbilityDescription() { return this.activeAbilityDescription; }
    public void setActiveAbilityDescription(String activeAbilityDescription) { this.activeAbilityDescription = activeAbilityDescription; }
    
    private String fieldAbilityName;
    
    public String getFieldAbilityName() { return this.fieldAbilityName; }
    public void setFieldAbilityName(String fieldAbilityName) { this.fieldAbilityName = fieldAbilityName; }
    
    private String conversationAbilityName;
    
    public String getConversationAbilityName() { return this.conversationAbilityName; }
    public void setConversationAbilityName(String conversationAbilityName) { this.conversationAbilityName = conversationAbilityName; }
    
    private Vector2 clickPointOffset;
    
    public Vector2 getClickPointOffset() { return this.clickPointOffset; }
    public void setClickPointOffset(Vector2 clickPointOffset) { this.clickPointOffset = clickPointOffset; }
    
    private String turnOnSoundId;
    
    public String getTurnOnSoundId() { return this.turnOnSoundId; }
    public void setTurnOnSoundId(String turnOnSoundId) { this.turnOnSoundId = turnOnSoundId; }
    
    private String turnOffSoundId;
    
    public String getTurnOffSoundId() { return this.turnOffSoundId; }
    public void setTurnOffSoundId(String turnOffSoundId) { this.turnOffSoundId = turnOffSoundId; }

    public void SaveToXml(CaseXmlWriter writer) throws IOException
    {
        writer.StartElement("Partner");
        writer.WriteTextElement("Id", this.id);
        writer.WriteTextElement("Name", this.name);
        
        if (this.iconSpriteId != null)
        {
            writer.WriteSpriteId("IconSpriteId", this.iconSpriteId);
        }
        
        if (this.profileImageSpriteId != null)
        {
            writer.WriteSpriteId("ProfileImageSpriteId", this.profileImageSpriteId);
        }
        
        if (this.passiveAbilityDescription != null)
        {
            writer.WriteTextElement("PassiveAbilityDescription", this.passiveAbilityDescription);
        }
        
        if (this.activeAbilityDescription != null)
        {
            writer.WriteTextElement("ActiveAbilityDescription", this.activeAbilityDescription);
        }
        
        if (this.fieldAbilityName != null)
        {
            writer.WriteTextElement("FieldAbilityName", this.fieldAbilityName);
        }
        
        if (this.conversationAbilityName != null)
        {
            writer.WriteTextElement("ConversationAbilityName", this.conversationAbilityName);
        }
        
        if (this.clickPointOffset != null)
        {
            writer.StartElement("ClickPointOffset");
            this.clickPointOffset.SaveToXml(writer);
            writer.EndElement();
            
            if (this.turnOnSoundId != null)
            {
                writer.WriteTextElement("TurnOnSoundId", this.turnOnSoundId);
            }
            
            if (this.turnOffSoundId != null)
            {
                writer.WriteTextElement("TurnOffSoundId", this.turnOffSoundId);
            }
            
            writer.StartElement("FieldCursorDefinitions");

            writer.StartElement("Entry");
            writer.WriteTextElement("State", FieldCursorState.None.name());
            this.fieldCursorDefinitionsByState.get(FieldCursorState.None).SaveToXml(writer);
            writer.EndElement();

            writer.StartElement("Entry");
            writer.WriteTextElement("State", FieldCursorState.Low.name());
            this.fieldCursorDefinitionsByState.get(FieldCursorState.Low).SaveToXml(writer);
            writer.EndElement();

            writer.StartElement("Entry");
            writer.WriteTextElement("State", FieldCursorState.Mid.name());
            this.fieldCursorDefinitionsByState.get(FieldCursorState.Mid).SaveToXml(writer);
            writer.EndElement();

            writer.StartElement("Entry");
            writer.WriteTextElement("State", FieldCursorState.High.name());
            this.fieldCursorDefinitionsByState.get(FieldCursorState.High).SaveToXml(writer);
            writer.EndElement();

            writer.StartElement("Entry");
            writer.WriteTextElement("State", FieldCursorState.Extreme.name());
            this.fieldCursorDefinitionsByState.get(FieldCursorState.Extreme).SaveToXml(writer);
            writer.EndElement();

            writer.StartElement("Entry");
            writer.WriteTextElement("State", FieldCursorState.Overlay.name());
            this.fieldCursorDefinitionsByState.get(FieldCursorState.Overlay).SaveToXml(writer);
            writer.EndElement();

            writer.EndElement();
        }
        
        writer.StartElement("PartnerConversations");
        
        for (PartnerConversation conversation : this.conversationList)
        {
            writer.StartElement("Entry");
            
            writer.StartElement("Condition");
            conversation.getCondition().SaveToXml(writer);
            writer.EndElement();
            
            writer.StartElement("Conversation");
            conversation.getConveration().SaveToXml(writer);
            writer.EndElement();
            
            writer.EndElement();
        }
        
        writer.EndElement();
        
        writer.EndElement();
    }

    public Partner(CaseXmlReader reader)
    {
        reader.StartElement("Partner");
        this.id = reader.ReadTextElement("Id");
        this.name = reader.ReadTextElement("Name");
        
        if (reader.ElementExists("IconSpriteId"))
        {
            this.iconSpriteId = reader.ReadTextElement("IconSpriteId");
        }
        
        if (reader.ElementExists("ProfileImageSpriteId"))
        {
            this.profileImageSpriteId = reader.ReadTextElement("ProfileImageSpriteId");
        }
        
        if (reader.ElementExists("PassiveAbilityDescription"))
        {
            this.passiveAbilityDescription = reader.ReadTextElement("PassiveAbilityDescription");
        }
        
        if (reader.ElementExists("ActiveAbilityDescription"))
        {
            this.activeAbilityDescription = reader.ReadTextElement("ActiveAbilityDescription");
        }
        
        if (reader.ElementExists("ConversationAbilityName"))
        {
            this.conversationAbilityName = reader.ReadTextElement("ConversationAbilityName");
        }
        
        reader.EndElement();
    }

    public void markChildSprites(String parentId) throws CaseParseException
    {
        if (this.getIconSprite() != null)
        {
            this.getIconSprite().markChildSprites(parentId);
        }
        
        if (this.getProfileImageSprite() != null)
        {
            this.getProfileImageSprite().markChildSprites(parentId);
        }
        
        if (this.clickPointOffset != null)
        {
            Case.getInstance().getAnimationManager().GetAnimationFromId(this.fieldCursorDefinitionsByState.get(FieldCursorState.None).getAnimationId()).markChildSprites(parentId);
            Case.getInstance().getAnimationManager().GetAnimationFromId(this.fieldCursorDefinitionsByState.get(FieldCursorState.Low).getAnimationId()).markChildSprites(parentId);
            Case.getInstance().getAnimationManager().GetAnimationFromId(this.fieldCursorDefinitionsByState.get(FieldCursorState.Mid).getAnimationId()).markChildSprites(parentId);
            Case.getInstance().getAnimationManager().GetAnimationFromId(this.fieldCursorDefinitionsByState.get(FieldCursorState.High).getAnimationId()).markChildSprites(parentId);
            Case.getInstance().getAnimationManager().GetAnimationFromId(this.fieldCursorDefinitionsByState.get(FieldCursorState.Extreme).getAnimationId()).markChildSprites(parentId);
            Case.getInstance().getAnimationManager().GetAnimationFromId(this.fieldCursorDefinitionsByState.get(FieldCursorState.Overlay).getAnimationId()).markChildSprites(parentId);
        }
        
        for (PartnerConversation conversation : this.conversationList)
        {
            conversation.getConveration().markChildSprites(parentId);
        }
    }
}